﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using ThresholdAssistant.DataContract.Result;
using ThresholdAssistant.WebAPI.Models;
using ThresholdAssistant.WebAPI.Models.Result.Data;
using ThresholdAssistant.WebAPI.Service;

namespace ThresholdAssistant.WebAPI.Controllers
{
    /// <summary>
    /// 数据录入控制器
    /// </summary>
    [Route("api/record")]
    public class RecordController : BaseController
    {
        /// <summary>
        /// 数据录入服务
        /// </summary>
        public RecordService RecordService { get; set; }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("get-list")]
        [AllowAnonymous]
        public async Task<ContractResult<IEnumerable<object>>> GetListAsync(GetRecordListParam param)
        {
            return await RecordService.GetListAsync(param);
        }

        /// <summary>
        /// 汇总
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("get-vlist")]
        [AllowAnonymous]
        public async Task<ContractResult<IEnumerable<VRecordData>>> GetVListAsync(GetRecordListParam param)
        {
            return await RecordService.GetVListAsync(param);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("add")]
        [AllowAnonymous]
        public async Task<ContractResult<long>> AddAsync(AddRecordParam param)
        {
            //数据录入
            var result = await RecordService.AddAsync(param);
            //阈值提醒添加
            await RecordService.CheckThreshold(param);

            return result;
        }
        /// <summary>
        /// 心跳
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("heartbeat")]
        [AllowAnonymous]
        public async Task<ContractResult<SmsInfoParam>> HeartbeatAsync(GetThresholdListParam param)
        {
            var result = new ContractResult<SmsInfoParam>();
            var smsInfo = ThresholdFileService.RemoveSms(param.GroupId.ToString());
            result.Data = smsInfo;
            return result.SetSuceccful();
        }

        /// <summary>
        /// 单个
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("get-info")]
        [AllowAnonymous]
        public async Task<ContractResult<RecordData>> GetInfoAsync(GetRecordInfoParam param)
        {
            return await RecordService.GetInfoAsync(param.Id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("update")]
        [AllowAnonymous]
        public async Task<ContractResult> UpdateAsync(UpdateRecordParam param)
        {
            return await RecordService.UpdateAsync(param);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        [AllowAnonymous]
        public async Task<ContractResult> DeleteAsync(DeleteRecordParam param)
        {
            return await RecordService.DeleteAsync(param.Id);
        }
    }
}
